package com.qbw.app.module.vehicle.holder;

import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lib.util.ScreenUtil;
import com.qbw.app.R;
import com.qbw.app.XApplication;
import com.qbw.app.databinding.VehicleHolderEventTypeBinding;
import com.qbw.app.model.EventType;
import com.qbw.app.module.vehicle.adapter.VehicleAdapter;
import com.qbw.core.base.BaseHolder;
import com.qbw.recyclerview.expandable.ExpandableAdapter;

/**
 * @author QBW
 * @date 2019/9/3
 */
public class VehicleEventTypeHolder extends BaseHolder<EventType, VehicleHolderEventTypeBinding> {

    public VehicleEventTypeHolder(Context context, ViewGroup viewParent, Object listener) {
        super(context, R.layout.vehicle_holder_event_type, viewParent, listener);
        mBinding.rcvSubType.setLayoutManager(new GridLayoutManager(context, 3));
        mBinding.rcvSubType.addItemDecoration(new InnerDecoration());
    }

    @Override
    public void bindData(int position, EventType data) {
        super.bindData(position, data);
        mBinding.setEvent(data);
        mBinding.executePendingBindings();
        VehicleAdapter vehicleAdapter = new VehicleAdapter(mContext, mListener);
        vehicleAdapter.addChild(data.getSubEventTypes());
        mBinding.rcvSubType.setAdapter(vehicleAdapter);
    }

    private static class InnerDecoration extends RecyclerView.ItemDecoration {

        private Context mContext = XApplication.getInstance();
        private int mSpace15;
        private int mSpace19;

        public InnerDecoration() {
            mSpace15 = ScreenUtil.dp2px(mContext, 15);
            mSpace19 = ScreenUtil.dp2px(mContext, 19);
        }

        @Override
        public void getItemOffsets(Rect outRect,
                                   View view,
                                   RecyclerView parent,
                                   RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            int ap = parent.getChildAdapterPosition(view);
            if (ap == RecyclerView.NO_POSITION) {
                return;
            }
            ExpandableAdapter adapter = (ExpandableAdapter) parent.getAdapter();
            int childPos = adapter.getChildPosition(ap);
            int per4 = (int) (mSpace15 / 3F);
            switch (childPos % 3) {
                case 0:
                    outRect.left = mSpace15;
                    outRect.right = per4;
                    break;
                case 1:
                    outRect.left = per4 * 2;
                    outRect.right = per4 * 2;
                    break;
                case 2:
                    outRect.left = per4;
                    outRect.right = mSpace15;
                    break;
                default:
                    break;
            }
            outRect.bottom = mSpace19;
        }
    }
}
